package test.test.test;


import javax.microedition.khronos.opengles.GL10;

import org.anddev.andengine.engine.Engine;
import org.anddev.andengine.engine.camera.Camera;
import org.anddev.andengine.engine.handler.timer.ITimerCallback;
import org.anddev.andengine.engine.handler.timer.TimerHandler;
import org.anddev.andengine.engine.options.EngineOptions;
import org.anddev.andengine.engine.options.EngineOptions.ScreenOrientation;
import org.anddev.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.anddev.andengine.entity.IEntity;
import org.anddev.andengine.entity.modifier.FadeInModifier;
import org.anddev.andengine.entity.modifier.FadeOutModifier;
import org.anddev.andengine.entity.modifier.IEntityModifier.IEntityModifierListener;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.scene.background.ColorBackground;
import org.anddev.andengine.entity.scene.menu.MenuScene;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.entity.util.FPSLogger;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.anddev.andengine.opengl.texture.region.TextureRegion;
import org.anddev.andengine.ui.activity.BaseGameActivity;
import org.anddev.andengine.util.modifier.IModifier;
import org.anddev.andengine.util.modifier.ease.EaseLinear;

import test.test.R;
import android.content.Intent;
import android.view.KeyEvent;

public class splashScreen extends BaseGameActivity {
	// ===========================================================
	// Constants
	// ===========================================================

	private static final int CAMERA_WIDTH = 720;
	private static final int CAMERA_HEIGHT = 480;

	protected static final int MENU_RESET = 0;
	protected static final int MENU_QUIT = MENU_RESET + 1;

	// ===========================================================
	// Fields
	// ===========================================================

	protected Camera mCamera;

	protected Scene mMainScene;

	private BitmapTextureAtlas mTexture;
	private TextureRegion mFaceTextureRegion;

	protected MenuScene mMenuScene;

	protected TextureRegion mMenuResetTextureRegion;
	protected TextureRegion mMenuQuitTextureRegion;
	
	private Sprite splash;

	// ===========================================================
	// Constructors
	// ===========================================================

	// ===========================================================
	// Getter & Setter
	// ===========================================================

	// ===========================================================
	// Methods for/from SuperClass/Interfaces
	// ===========================================================

	public Engine onLoadEngine() {
		this.mCamera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
		
		return new Engine(new EngineOptions(true, ScreenOrientation.PORTRAIT, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), this.mCamera));
	}

	public void onLoadResources() {
		this.mTexture = new BitmapTextureAtlas(64, 64, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		this.mFaceTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mTexture, this, "robot.png", 0, 0);
		this.mEngine.getTextureManager().loadTexture(this.mTexture);
	}

	public Scene onLoadScene() {
		this.mEngine.registerUpdateHandler(new FPSLogger());

		//Initializes Global variables
		Options.DigitalControlEnabled = false;
		Options.TouchEnabled = true;
		
		/* Initializing the scene and creating a background */
		this.mMainScene = new Scene();
		this.mMainScene.setBackground(new ColorBackground(0, 0, 0));
		
		/* Calculate the coordinates for the face, so its centered on the camera. */
		final int centerX = (CAMERA_WIDTH - this.mFaceTextureRegion.getWidth()) / 2;
		final int centerY = (CAMERA_HEIGHT - this.mFaceTextureRegion.getHeight()) / 2;
		
		splash = new Sprite(centerX, centerY, this.mFaceTextureRegion);
		splash.setScale(4);
		this.mMainScene.attachChild(splash);
		
		FadeInModifier prFadeInModifier = new FadeInModifier(0.5f, new IEntityModifierListener() {
			public void onModifierStarted(IModifier<IEntity> pModifier, IEntity pItem) {
				

			}

			public void onModifierFinished(IModifier<IEntity> pModifier, IEntity pItem) {
				createSpriteSpawnTimeHandler();

			}
		}, EaseLinear.getInstance());

		//Apply the fade properties
		splash.setBlendFunction(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
		splash.registerEntityModifier(prFadeInModifier);
		
		return this.mMainScene;
	}


	@Override
	public void runOnUpdateThread(Runnable pRunnable) {
		super.runOnUpdateThread(pRunnable);
	}

	public void onLoadComplete() {
		
	}


	// ===========================================================
	// Methods
	// ===========================================================
	/**
	 * Creates a Timer Handler used to Spawn Sprites
	 */
	private void createSpriteSpawnTimeHandler(){
		
	        this.getEngine().registerUpdateHandler(new TimerHandler(1f, new ITimerCallback()
	        {                      
	            public void onTimePassed(final TimerHandler pTimerHandler)
	            {
	        		FadeOutModifier prFadeOutModifier = new FadeOutModifier(0.5f, new IEntityModifierListener() {
	        			public void onModifierStarted(IModifier<IEntity> pModifier, IEntity pItem) {
	        				

	        			}

	        			public void onModifierFinished(IModifier<IEntity> pModifier, IEntity pItem) {
	    	            	Intent intent = new Intent(splashScreen.this, MainMenu.class);
	    	            	startActivity(intent);
	    	            	finish();

	        			}
	        		}, EaseLinear.getInstance());

	        		//Apply the fade properties
	        		splash.setBlendFunction(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
	        		splash.registerEntityModifier(prFadeOutModifier);

	            }
	        }));
	}

	@Override
	public boolean onKeyDown(final int pKeyCode, final KeyEvent pEvent) {
		if(pKeyCode == KeyEvent.KEYCODE_BACK) {
			//If back is pressed than nothing happens
			return true;
		} else {
			return super.onKeyDown(pKeyCode, pEvent);
		}
	}



	// ===========================================================
	// Inner and Anonymous Classes
	// ===========================================================
	@Override
	protected void onPause() {
		super.onPause();
		splashScreen.this.overridePendingTransition(R.anim.fadein, R.anim.fadeout);
	}

}